package cn.com.zhangwoo.microstar.viewpager.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.PaintDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import cn.com.microstar.activity.R;
import cn.com.zhangwoo.microstar.homepage.activity.HomeSupermarketActivity;
import cn.com.zhangwoo.microstar.inteface.IUtilDB;
import cn.com.zhangwoo.microstar.utildb.Utils;
import cn.com.zhangwoo.microstar.widget.NoScrollGridView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;


public class HomePageViewPagerFragment extends Fragment implements OnItemClickListener {
    
    private NoScrollGridView mGridView;
    private String mCity;
    private DisplayImageOptions mDisplayImageOptions;
    public HomePageViewPagerFragment(){
        
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mDisplayImageOptions = Utils.buildDisplayImageOptions(getActivity(), new PaintDrawable(),
                new PaintDrawable(), new PaintDrawable(), new RoundedBitmapDisplayer(8));
        mGridView = new NoScrollGridView(getActivity());
        mGridView.setNumColumns(4);
        mGridView.setSelector(R.drawable.list_selector);//点击背景色
        MyHomeGridAdapter  adapter=new MyHomeGridAdapter(getActivity());
        //adapter=new MyHomeGridAdapter(getActivity(),homePageMesName);
        mGridView.setAdapter(adapter);
        mGridView.setOnItemClickListener(this);
        return mGridView;
    }
   
	

  
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    		showDateAdapterItem(position);
    }
    /**
     * 1:缓存记载
     * 2:直接加载应用
     */
    
   
    Intent intent;
    private void showDateAdapterItem(int position) {
    	switch (position) {
		case 1:
		    intent=new Intent(getActivity(),HomeSupermarketActivity.class);		
			startActivity(intent);	
			break;
		
		case 2:
			
			break;
		
		case 3:
			
			break;
		
		case 4:
			//intent=new Intent(getActivity(),LifeServiceMain.class);		
			startActivity(intent);	
			break;
	
		default:
			break;
		}
    	
	}
    
    
    class MyHomeGridAdapter extends BaseAdapter {

    	LayoutInflater inflater;
    	private Context context;
    	public MyHomeGridAdapter(Context context) {
    		this.inflater=LayoutInflater.from(context);
    	}
    	  @Override
          public int getCount() {
             return IUtilDB.HOMEIMG.length;
          }

          @Override
          public Object getItem(int position) {
              return IUtilDB.HOMEIMG[position];
          }

          @Override
          public long getItemId(int position) {
              return position;
          }


    	@SuppressLint("InflateParams") @Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		 ViewHolder holder = null;
             if (null == convertView) {
                 convertView =inflater.inflate(R.layout.homepage_viewpager_fragment_grid_item, null);
                 holder = new ViewHolder();
                 holder.image = (ImageView) convertView.findViewById(R.id.image);
                 holder.name = (TextView) convertView.findViewById(R.id.name);
                 convertView.setTag(holder);
             } else {
                 holder = (ViewHolder) convertView.getTag();
             }
             holder.name.setText(IUtilDB.HOMEVIEWPAGER[position]);
             holder.image.setImageResource(IUtilDB.HOMEIMG[position]);
             return convertView;
    	}
    	 
        class ViewHolder {
            ImageView image;
            TextView name;
        }
        
    }

}
